package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/admin/setmeal")
@RestController
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result deleteByIds(Long [] ids){
        Result result = setmealService.deleteByIds(ids);
        return result;
    }
    @GetMapping("/{setmealId}")
    @ApiOperation("根据id查询")
    public Result findById(@PathVariable Long setmealId){
        Result byId = setmealService.findById(setmealId);
        return byId;

    }
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO ){
        Result result = setmealService.updateSetmeal(setmealDTO);
        return result;
    }
    @PostMapping("/status/{status}")
    @ApiOperation("起售停售")
    @CacheEvict(value = "setmeal",allEntries = true)
    public Result startOrStop(@PathVariable Long status,@RequestParam("id") Long id){
        setmealService.startOrStop(status,id);
        return Result.success();
    }

}
